/*
 * SendFiles.java
 *
 * Created on 2008年10月30日, 下午7:50
 */

package ipmsg.gui;

import ipmsg.etc.Command;
import ipmsg.etc.FileLinkList;
import ipmsg.etc.FileNode;
import ipmsg.etc.GlobalConstant;
import ipmsg.etc.GlobalVar;
import ipmsg.network.UtilityNet;
import java.io.File;
import javax.swing.DefaultListModel;

/**
 *
 * @author  Noeru
 */
public class SendFiles extends javax.swing.JFrame {
    
    private String desIp;
    private String note=null;

    /** Creates new form SendFiles */
    public SendFiles(String desIp) {
        this.init(desIp);
    }
    
    public SendFiles(String remoteIP,File[]files){
        this.init(remoteIP);
        this.addFiles(files);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        guiSend = new javax.swing.JButton();
        guiCancel = new javax.swing.JButton();
        guiAdd = new javax.swing.JButton();
        guiDel = new javax.swing.JButton();
        guiSelectALL = new javax.swing.JCheckBox();
        jButton1 = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        guiAllFiles = new javax.swing.JList();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();
        jMenuItem3 = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JSeparator();
        jMenuItem5 = new javax.swing.JMenuItem();
        jMenuItem4 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        guiSend.setText("发送");
        guiSend.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                guiSendActionPerformed(evt);
            }
        });

        guiCancel.setText("退出");
        guiCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                guiCancelActionPerformed(evt);
            }
        });

        guiAdd.setText("添加");
        guiAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                guiAddActionPerformed(evt);
            }
        });

        guiDel.setText("删除");
        guiDel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                guiDelActionPerformed(evt);
            }
        });

        guiSelectALL.setText("全选");
        guiSelectALL.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                guiSelectALLItemStateChanged(evt);
            }
        });

        jButton1.setText("添加说明");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(guiSelectALL)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(guiAdd)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(guiDel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 33, Short.MAX_VALUE)
                .addComponent(jButton1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(guiCancel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(guiSend)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(guiAdd)
                    .addComponent(guiDel)
                    .addComponent(guiSelectALL)
                    .addComponent(guiSend)
                    .addComponent(guiCancel)
                    .addComponent(jButton1))
                .addContainerGap())
        );

        getContentPane().add(jPanel1, java.awt.BorderLayout.SOUTH);

        guiAllFiles.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                guiAllFilesValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(guiAllFiles);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 427, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 221, Short.MAX_VALUE)
        );

        getContentPane().add(jPanel2, java.awt.BorderLayout.CENTER);

        jMenu1.setText("文件");

        jMenuItem1.setText("添加文件");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem1);

        jMenuItem2.setText("删除文件");
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem2);

        jMenuItem3.setText("清空列表");
        jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem3ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem3);
        jMenu1.add(jSeparator1);

        jMenuItem5.setText("发送");
        jMenuItem5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem5ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem5);

        jMenuItem4.setText("退出");
        jMenuItem4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem4ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem4);

        jMenuBar1.add(jMenu1);

        setJMenuBar(jMenuBar1);

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-435)/2, (screenSize.height-318)/2, 435, 318);
    }// </editor-fold>//GEN-END:initComponents

private void guiSendActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_guiSendActionPerformed
// TODO add your handling code here:
    this.sendFiles();
}//GEN-LAST:event_guiSendActionPerformed

private void guiCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_guiCancelActionPerformed
// TODO add your handling code here:
    this.dispose();
}//GEN-LAST:event_guiCancelActionPerformed

private void guiAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_guiAddActionPerformed

    File[] files;
    if((files=UtilityGui.chooseFile(this))!=null){
        this.addFiles(files);
    }
}//GEN-LAST:event_guiAddActionPerformed

private void guiDelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_guiDelActionPerformed
// TODO add your handling code here:
    this.delFiles();
}//GEN-LAST:event_guiDelActionPerformed

private void guiSelectALLItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_guiSelectALLItemStateChanged
// TODO add your handling code here:
    
    if(this.guiSelectALL.isSelected()){
        int len;
        if((len=this.guiAllFiles.getModel().getSize())>0){
            int []indices =new int[len];
            for(int i=0;i<len;i++)
                indices[i]=i;
            this.guiAllFiles.setSelectedIndices(indices);
        }
    }else this.guiAllFiles.clearSelection();
}//GEN-LAST:event_guiSelectALLItemStateChanged

private void guiAllFilesValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_guiAllFilesValueChanged
// TODO add your handling code here:
    int selectLen=this.guiAllFiles.getSelectedIndices().length;
    if(selectLen>0)this.guiDel.setEnabled(true);
    else this.guiDel.setEnabled(false);
}//GEN-LAST:event_guiAllFilesValueChanged

private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
// TODO add your handling code here:
    File[] files;
    if((files=UtilityGui.chooseFile(this))!=null){
        this.addFiles(files);
    }
}//GEN-LAST:event_jMenuItem1ActionPerformed

private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed
// TODO add your handling code here:
    this.delFiles();
}//GEN-LAST:event_jMenuItem2ActionPerformed

private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem3ActionPerformed
// TODO add your handling code here:
    this.guiAllFiles.setModel(new DefaultListModel());
}//GEN-LAST:event_jMenuItem3ActionPerformed

private void jMenuItem5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem5ActionPerformed
// TODO add your handling code here:
    this.sendFiles();
}//GEN-LAST:event_jMenuItem5ActionPerformed

private void jMenuItem4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem4ActionPerformed
// TODO add your handling code here:
    this.dispose();
}//GEN-LAST:event_jMenuItem4ActionPerformed

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
// TODO add your handling code here:
    this.note=UtilityGui.getInputString(this,"输入您的说明", "添加说明信息",
            GlobalConstant.MSG_LEN);
}//GEN-LAST:event_jButton1ActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton guiAdd;
    private javax.swing.JList guiAllFiles;
    private javax.swing.JButton guiCancel;
    private javax.swing.JButton guiDel;
    private javax.swing.JCheckBox guiSelectALL;
    private javax.swing.JButton guiSend;
    private javax.swing.JButton jButton1;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JMenuItem jMenuItem4;
    private javax.swing.JMenuItem jMenuItem5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    // End of variables declaration//GEN-END:variables

    private void init(String desIp){
        
        this.desIp=desIp;
        initComponents();
        this.guiDel.setEnabled(false);
        this.setTitle("发送文件至"+GlobalVar.getUser(this.desIp).getName()+
                " ( "+this.desIp+" ) ");
        
        MyTransferHandler handler = new MyTransferHandler(this.guiAllFiles);
        this.setTransferHandler(handler);
        this.guiAllFiles.setTransferHandler(handler);

    }
    
    
    private void addFiles(File[] files) {

        if (files == null) {
            return;
        }
        DefaultListModel listModel;
        if (this.guiAllFiles.getModel().getSize() == 0) {
            listModel = new DefaultListModel();
        } else {
            listModel = (DefaultListModel) this.guiAllFiles.getModel();
        }
        for (int i = files.length - 1; i >= 0; i--) {
            if (!listModel.contains(files[i].getPath())) {
                listModel.addElement(files[i].getPath());
            }
        }
        this.guiAllFiles.setModel(listModel);
        this.guiDel.setEnabled(true);

        if (this.guiAllFiles.getModel().getSize() > 0) {
            this.guiAllFiles.setSelectedIndex(0);
        }
    }
    
    private void delFiles(){

        int[] o = this.guiAllFiles.getSelectedIndices();
        if (o.length == 0) {
            return;
        }
        DefaultListModel listModel =
                (DefaultListModel) this.guiAllFiles.getModel();
        for (int i = o.length - 1; i >= 0; i--) {
            listModel.remove(o[i]);
        }
        this.guiAllFiles.setModel(listModel);

        if (this.guiAllFiles.getModel().getSize() == 0) {
            this.guiDel.setEnabled(false);
        }
        if (this.guiAllFiles.getModel().getSize() > 0) {
            this.guiAllFiles.setSelectedIndex(0);
        }
    }
    
    private void sendFiles(){
        if(this.guiAllFiles.getModel().getSize()==0){
            UtilityGui.showNotice(this, "请添加文件");
            return;
        }
        
        Command com=new Command(GlobalConstant.IPMSG_SENDMSG|
                GlobalConstant.IPMSG_FILEATTACHOPT);
        FileLinkList flist=new FileLinkList();
        flist.setIp(this.desIp);
        flist.setPacketNo(com.getPacketNo());
        
        File file;
        DefaultListModel listModel=(DefaultListModel) 
                this.guiAllFiles.getModel();
        for(int i=0;i<listModel.getSize();){
            file=new File(((String)listModel.get(i)).trim());
            FileNode fnode=new  FileNode();
            if(file.isFile()){
                fnode.setFileNo(i);
                fnode.setFileName(file.getPath());
                fnode.setFileLen(file.length());
                fnode.setFileKind(GlobalConstant.IPMSG_FILE_REGULAR);
                flist.getFiles().add(fnode);
                i++;
            }
            else if(file.isDirectory()){
                fnode.setFileNo(i);
                fnode.setFileName(file.getPath());
                fnode.setFileLen(file.length());
                fnode.setFileKind(GlobalConstant.IPMSG_FILE_DIR);
                flist.getFiles().add(fnode);
                i++;
            }
        }
        if(this.note==null)this.note="";
        com.setIp(this.desIp);
        com.setAdditional(this.note+"\0" + flist.exportPacket());
        UtilityNet.sendUdpPacket(com);
        GlobalVar.addFileList(flist);
        this.dispose();
    }
}
